package org.jeecg.modules.pm.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.pm.entity.PmRouteData;
import org.jeecg.modules.pm.entity.PmRouteDiseases;
import org.jeecg.modules.pm.entity.PmRouteDiseases100;
import org.jeecg.modules.pm.service.IPmRouteDataService;
import org.jeecg.modules.pm.service.IPmRouteDiseases100Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.pm.util.PmExcelUtil;
import org.jeecg.modules.pm.vo.ProjectHandleVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 路线百米病害
 * @@author 品讯科技
 * @Date: 2023-12-17
 * @Version: V1.0
 */
@Api(tags = "路线百米病害")
@RestController
@RequestMapping("/pm/pmRouteDiseases100")
@Slf4j
public class PmRouteDiseases100Controller extends BaseController<PmRouteDiseases100, IPmRouteDiseases100Service> {
    @Autowired
    private IPmRouteDiseases100Service pmRouteDiseases100Service;
    @Autowired
    private IPmRouteDataService pmRouteDataService;

    /**
     * 分页列表查询
     *
     * @param pmRouteDiseases100
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "路线百米病害-分页列表查询")
    @ApiOperation(value = "路线百米病害-分页列表查询", notes = "路线百米病害-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<PmRouteDiseases100>> queryPageList(PmRouteDiseases100 pmRouteDiseases100,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<PmRouteDiseases100> queryWrapper = QueryGenerator.initQueryWrapper(pmRouteDiseases100, req.getParameterMap());
        Page<PmRouteDiseases100> page = new Page<PmRouteDiseases100>(pageNo, pageSize);
        IPage<PmRouteDiseases100> pageList = pmRouteDiseases100Service.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param pmRouteDiseases100
     * @return
     */
    @AutoLog(value = "路线百米病害-添加")
    @ApiOperation(value = "路线百米病害-添加", notes = "路线百米病害-添加")
    //@RequiresPermissions("pm:pm_route_diseases100:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody PmRouteDiseases100 pmRouteDiseases100) {
        pmRouteDiseases100Service.save(pmRouteDiseases100);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param pmRouteDiseases100
     * @return
     */
    @AutoLog(value = "路线百米病害-编辑")
    @ApiOperation(value = "路线百米病害-编辑", notes = "路线百米病害-编辑")
    //@RequiresPermissions("pm:pm_route_diseases100:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody PmRouteDiseases100 pmRouteDiseases100) {
        pmRouteDiseases100Service.updateById(pmRouteDiseases100);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "路线百米病害-通过id删除")
    @ApiOperation(value = "路线百米病害-通过id删除", notes = "路线百米病害-通过id删除")
    //@RequiresPermissions("pm:pm_route_diseases100:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        pmRouteDiseases100Service.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "路线百米病害-批量删除")
    @ApiOperation(value = "路线百米病害-批量删除", notes = "路线百米病害-批量删除")
    //@RequiresPermissions("pm:pm_route_diseases100:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.pmRouteDiseases100Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "路线百米病害-通过id查询")
    @ApiOperation(value = "路线百米病害-通过id查询", notes = "路线百米病害-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<PmRouteDiseases100> queryById(@RequestParam(name = "id", required = true) String id) {
        PmRouteDiseases100 pmRouteDiseases100 = pmRouteDiseases100Service.getById(id);
        if (pmRouteDiseases100 == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(pmRouteDiseases100);
    }


    /**
     * 计算道路数据  计算A和B的两个年度
     *
     * @param routeId
     * @return
     */
    @AutoLog(value = "路线百米病害-计算道路修补率")
    @ApiOperation(value = "路线百米病害-计算道路数据", notes = "路线百米病害-计算道路数据")
    @PostMapping(value = "/calculation/{routeId}")
    public Result<String> calculation(@PathVariable(name = "routeId") String routeId) {
        pmRouteDiseases100Service.calculationRoute(routeId);
        return Result.OK("计算成功！");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param pmRouteDiseases100
     */
    //@RequiresPermissions("pm:pm_route_diseases100:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PmRouteDiseases100 pmRouteDiseases100) {
        return super.exportXls(request, pmRouteDiseases100, PmRouteDiseases100.class, "路线百米病害");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("pm:pm_route_diseases100:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PmRouteDiseases100.class);
    }

    /**
     * 病害导入
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importDiseasesExcel", method = RequestMethod.POST)
    public Result<?> importDiseasesExcel(HttpServletRequest request, HttpServletResponse response) {
        //路线ID
        String routeId = request.getParameter("routeId");
        //年份
        String year = request.getParameter("year");
        if (StrUtil.isBlank(routeId) || StrUtil.isBlank(year)) {
            return Result.error("文件导入失败！不存在的路线");
        }

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<PmRouteDiseases100> list = ExcelImportUtil.importExcel(file.getInputStream(), PmRouteDiseases100.class, params);
                //重新导入数据的话则把已有的数据全部置空
                if (!CollectionUtils.isEmpty(list)) {
                    //先删除路线病害信息
                    LambdaQueryWrapper<PmRouteDiseases100> deleteLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    deleteLambdaQueryWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                    deleteLambdaQueryWrapper.eq(PmRouteDiseases100::getYear, year);
                    this.pmRouteDiseases100Service.remove(deleteLambdaQueryWrapper);
                }

                for (PmRouteDiseases100 pmRouteDiseases : list) {
                    //垃圾数据不处理
                    if (StrUtil.isBlank(pmRouteDiseases.getStart()) || StrUtil.isBlank(pmRouteDiseases.getFinish())) {
                        continue;
                    }
                    //主要数据格式
                    Integer meter = Double.valueOf(pmRouteDiseases.getStart().replace("K", "").replace("k", "").replace("+", "")).intValue();
                    pmRouteDiseases.setMeter100(meter / 100);
                    pmRouteDiseases.setStationNum(meter / 1000);
                    pmRouteDiseases.setRouteId(routeId);
                    pmRouteDiseases.setYear(year);

                    //设置条修和快修的面积
                    pmRouteDiseases.setBlockArea(NumberUtil.add(pmRouteDiseases.getKx(), pmRouteDiseases.getTx()));
                    //设置其他病害面积
                    pmRouteDiseases.setOtherArea(NumberUtil.add(pmRouteDiseases.getJl(), pmRouteDiseases.getZxlf(),
                            pmRouteDiseases.getHxlf(), pmRouteDiseases.getCx(), pmRouteDiseases.getCz(), pmRouteDiseases.getBlyb(),
                            pmRouteDiseases.getKc(), pmRouteDiseases.getSs(), pmRouteDiseases.getFy()).doubleValue());

                }
                long start = System.currentTimeMillis();
                this.pmRouteDiseases100Service.saveBatch(list);

                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
                return Result.ok("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {

                String msg = e.getMessage();
                log.error(msg, e);
                if (msg != null && msg.indexOf("Duplicate entry") >= 0) {
                    return Result.error("文件导入失败:有重复数据！");
                } else {
                    return Result.error("文件导入失败:" + e.getMessage());
                }
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    /**
     * 导入当前年度方案数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importProjectExcel", method = RequestMethod.POST)
    public Result<?> importProjectExcel(HttpServletRequest request, HttpServletResponse response) {
        //路线ID
        String routeId = request.getParameter("routeId");
        //年份
        String year = request.getParameter("year");
        if (StrUtil.isBlank(routeId) || StrUtil.isBlank(year)) {
            return Result.error("文件导入失败！不存在的路线");
        }

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setNeedSave(false);
            try {
                List<ProjectHandleVo> list = ExcelImportUtil.importExcel(file.getInputStream(), ProjectHandleVo.class, params);

                //重新导入方案的话则把已有的方案全部置空
                if (!CollectionUtils.isEmpty(list)) {
                    LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                    lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                    lambdaUpdateWrapper.eq(PmRouteDiseases100::getYear, year);
                    lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextBlockArea, null);
                    lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextOtherArea, null);
                    lambdaUpdateWrapper.set(PmRouteDiseases100::getProjectNext, "");
                    this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                    this.pmRouteDataService.removeProject(routeId);
                }

                for (ProjectHandleVo projectHandleVo : list) {
                    if (StrUtil.isBlank(projectHandleVo.getProject()) || StrUtil.isBlank(projectHandleVo.getStart()) || StrUtil.isBlank(projectHandleVo.getFinish())) {
                        continue;
                    }
                    //主要数据格式
                    //开始里程
                    Integer startStationNum = Integer.parseInt(projectHandleVo.getStart().replace("K", "").replace("k", "").replace("+", ""));
                    //结束里程
                    Integer  finishStationNum= Integer.parseInt(projectHandleVo.getFinish().replace("K", "").replace("k", "").replace("+", ""));

                    if(startStationNum>finishStationNum){
                        Integer temp = startStationNum;
                        startStationNum = finishStationNum;
                        finishStationNum = temp;
                    }


                    //计算出前置还剩余多少米
                    int startRemainderMeter = startStationNum % 100;
                    //隶属哪个百米段
                    int startMeter = startStationNum / 100;
                    //计算出后置还剩余多少米
                    int finishRemainderMeter = finishStationNum % 100;
                    //隶属哪个百米段
                    int finishMeter = finishStationNum / 100;

                    //1、处理开始第一段数据
                    if ((startRemainderMeter >= 50 && startMeter == finishMeter) || startRemainderMeter < 50) {
                        //如果修补在同一个百米段 或者 如果数据小于50  则说明100米段需要把方案赋上
                        LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getYear, year);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, startMeter);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getProjectNext, projectHandleVo.getProject());
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextBlockArea, 0.0F);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextOtherArea, 0.0F);
                        this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                        this.pmRouteDataService.updateProject(routeId,startMeter,projectHandleVo.getProject());
                    } else if (startRemainderMeter >= 50 && startMeter != finishMeter) {
                        //如果修补不在同一个百米段
                        LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getYear, year);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, startMeter + 1);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getProjectNext, projectHandleVo.getProject());
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextBlockArea, 0.0F);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextOtherArea, 0.0F);
                        this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                        this.pmRouteDataService.updateProject(routeId,startMeter + 1,projectHandleVo.getProject());
                    }

                    //2、处理连续段数据
                    if (startMeter < finishMeter) {
                        for (int i = startMeter + 1; i < finishMeter; i++) {
                            LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                            lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                            lambdaUpdateWrapper.eq(PmRouteDiseases100::getYear, year);
                            lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, i);
                            lambdaUpdateWrapper.set(PmRouteDiseases100::getProjectNext, projectHandleVo.getProject());
                            lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextBlockArea, 0.0F);
                            lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextOtherArea, 0.0F);
                            this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                            this.pmRouteDataService.updateProject(routeId,i,projectHandleVo.getProject());
                        }
                    }
                    //3、处理最后一段数据
                    if ((finishRemainderMeter < 50 && startMeter == finishMeter) || (finishRemainderMeter >= 50 && startMeter != finishMeter)) {
                        //如果数据小于50米 ，并且开始和结束都在同一个百米段中 或者修复超过50米并且不在同一百米路段
                        LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getYear, year);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, finishMeter);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getProjectNext, projectHandleVo.getProject());
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextBlockArea, 0.0F);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextOtherArea, 0.0F);
                        this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                        this.pmRouteDataService.updateProject(routeId,finishMeter,projectHandleVo.getProject());

                    } else if (finishRemainderMeter < 50 && startMeter != finishMeter) {
                        //如果数据小于50米 ，并且开始和结束都在同一个百米段中
                        LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getYear, year);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, finishMeter - 1);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getProjectNext, projectHandleVo.getProject());
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextBlockArea, 0.0F);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastNextOtherArea, 0.0F);
                        this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                        this.pmRouteDataService.updateProject(routeId,finishMeter - 1,projectHandleVo.getProject());
                    }
                }
                long start = System.currentTimeMillis();
                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
                return Result.ok("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {

                String msg = e.getMessage();
                log.error(msg, e);
                if (msg != null && msg.indexOf("Duplicate entry") >= 0) {
                    return Result.error("文件导入失败:有重复数据！");
                } else {
                    return Result.error("文件导入失败:" + e.getMessage());
                }
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }


    /**
     * 导入上一年方案
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importPrevProjectExcel", method = RequestMethod.POST)
    public Result<?> importPrevProjectExcel(HttpServletRequest request, HttpServletResponse response) {
        //路线ID
        String routeId = request.getParameter("routeId");
        //年份
        String year = request.getParameter("year");
        if (StrUtil.isBlank(routeId) || StrUtil.isBlank(year)) {
            return Result.error("文件导入失败！不存在的路线");
        }

        String prevYear = String.valueOf((Integer.parseInt(year)-1));
        List<String> yearList = Arrays.asList(year,prevYear);


        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setNeedSave(false);
            try {
                List<ProjectHandleVo> list = ExcelImportUtil.importExcel(file.getInputStream(), ProjectHandleVo.class, params);

                //重新导入方案的话则把已有的方案全部置空
                if (!CollectionUtils.isEmpty(list)) {
                    LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                    lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                    lambdaUpdateWrapper.in(PmRouteDiseases100::getYear, yearList);
                    lambdaUpdateWrapper.set(PmRouteDiseases100::getLastBlockArea, null);
                    lambdaUpdateWrapper.set(PmRouteDiseases100::getLastOtherArea, null);
                    lambdaUpdateWrapper.set(PmRouteDiseases100::getProject, "");
                    this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                }

                for (ProjectHandleVo projectHandleVo : list) {
                    if (StrUtil.isBlank(projectHandleVo.getProject()) || StrUtil.isBlank(projectHandleVo.getStart()) || StrUtil.isBlank(projectHandleVo.getFinish())) {
                        continue;
                    }
                    //主要数据格式
                    //开始里程
                    Integer startStationNum = Integer.parseInt(projectHandleVo.getStart().replace("K", "").replace("k", "").replace("+", ""));
                    //结束里程
                    Integer finishStationNum = Integer.parseInt(projectHandleVo.getFinish().replace("K", "").replace("k", "").replace("+", ""));

                    if(startStationNum>finishStationNum){
                        Integer temp = startStationNum;
                        startStationNum = finishStationNum;
                        finishStationNum = temp;
                    }

                    //计算出前置还剩余多少米
                    int startRemainderMeter = startStationNum % 100;
                    //隶属哪个百米段
                    int startMeter = startStationNum / 100;
                    //计算出后置还剩余多少米
                    int finishRemainderMeter = finishStationNum % 100;
                    //隶属哪个百米段
                    int finishMeter = finishStationNum / 100;

                    //1、处理开始第一段数据
                    if ((startRemainderMeter >= 50 && startMeter == finishMeter) || startRemainderMeter < 50) {
                        //如果修补在同一个百米段 或者 如果数据小于50  则说明100米段需要把方案赋上
                        LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                        lambdaUpdateWrapper.in(PmRouteDiseases100::getYear, yearList);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, startMeter);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getProject, projectHandleVo.getProject());
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastBlockArea, 0.0F);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastOtherArea, 0.0F);
                        this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                    } else if (startRemainderMeter >= 50 && startMeter != finishMeter) {
                        //如果修补不在同一个百米段
                        LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                        lambdaUpdateWrapper.in(PmRouteDiseases100::getYear, yearList);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, startMeter + 1);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getProject, projectHandleVo.getProject());
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastBlockArea, 0.0F);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastOtherArea, 0.0F);
                        this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                    }

                    //2、处理连续段数据
                    if (startMeter < finishMeter) {
                        for (int i = startMeter + 1; i < finishMeter; i++) {
                            LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                            lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                            lambdaUpdateWrapper.in(PmRouteDiseases100::getYear, yearList);
                            lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, i);
                            lambdaUpdateWrapper.set(PmRouteDiseases100::getProject, projectHandleVo.getProject());
                            lambdaUpdateWrapper.set(PmRouteDiseases100::getLastBlockArea, 0.0F);
                            lambdaUpdateWrapper.set(PmRouteDiseases100::getLastOtherArea, 0.0F);
                            this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                        }
                    }
                    //3、处理最后一段数据
                    if ((finishRemainderMeter < 50 && startMeter == finishMeter) || (finishRemainderMeter >= 50 && startMeter != finishMeter)) {
                        //如果数据小于50米 ，并且开始和结束都在同一个百米段中 或者修复超过50米并且不在同一百米路段
                        LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                        lambdaUpdateWrapper.in(PmRouteDiseases100::getYear, yearList);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, finishMeter);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getProject, projectHandleVo.getProject());
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastBlockArea, 0.0F);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastOtherArea, 0.0F);
                        this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);

                    } else if (finishRemainderMeter < 50 && startMeter != finishMeter) {
                        //如果数据小于50米 ，并且开始和结束都在同一个百米段中
                        LambdaUpdateWrapper<PmRouteDiseases100> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getRouteId, routeId);
                        lambdaUpdateWrapper.in(PmRouteDiseases100::getYear, yearList);
                        lambdaUpdateWrapper.eq(PmRouteDiseases100::getMeter100, finishMeter - 1);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getProject, projectHandleVo.getProject());
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastBlockArea, 0.0F);
                        lambdaUpdateWrapper.set(PmRouteDiseases100::getLastOtherArea, 0.0F);
                        this.pmRouteDiseases100Service.update(lambdaUpdateWrapper);
                    }
                }
                long start = System.currentTimeMillis();
                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
                return Result.ok("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {

                String msg = e.getMessage();
                log.error(msg, e);
                if (msg != null && msg.indexOf("Duplicate entry") >= 0) {
                    return Result.error("文件导入失败:有重复数据！");
                } else {
                    return Result.error("文件导入失败:" + e.getMessage());
                }
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

}
